package com.twu.biblioteca;

import java.io.InputStream;
import java.util.Scanner;

public class BibliotecaApp {

    private static String input;
    private static InputStream is;
    private static Scanner scanner;
    private static Library library;
    private static UserList userList;
    private static Customer currentUser;

    public static void main(String[] args) {
        library = new Library();
        userList = new UserList();
        MovieList movieList = new MovieList();
        currentUser = new Customer();
        is = System.in;
        scanner= new Scanner(is);

        Show.Message("Hi consumer, welcome to Biblioteca!");
        Show.HomePage();

        System.out.print("Please input your library number :");
        String libraryNum = scanner.nextLine();
        System.out.print("Please input your password :");
        String password = scanner.nextLine();

        currentUser = Login(libraryNum, password);
        if (currentUser != null) {
            while(true){
                System.out.print("your option :");
                input = scanner.nextLine();
                if (input.equals("Quit")) {
                    break;
                }
                if (input.equals("List Books")) {

                    ListBooksAction();
                    Show.HomePage();
                    continue;
                }
                if (input.equals("List Movies")){

                    ListMovieAction();
                    Show.HomePage();
                    continue;
                }
                if (input.equals("User Info")){

                    Show.UserInfo(currentUser);
                    Show.HomePage();
                    continue;
                }

                Show.Message("Select an invalid option!");
            }
        }
    }

    private static void ListMovieAction() {
        Show.Message("your check-out movie id :");
        input = scanner.nextLine();
        MovieList.CheckOut(input, currentUser);

        Show.Message("your return movie id :");
        input = scanner.nextLine();
        MovieList.Return(input, currentUser);
    }

    private static void ListBooksAction() {
        Show.Message("your check-out book id :");
        input = scanner.nextLine();
        Library.CheckOut(input, currentUser);

        Show.Message("your return book id :");
        input = scanner.nextLine();
        Library.Return(input, currentUser);
    }

    static Customer Login(String identity, String password) {
        Customer customer = UserList.Get(identity);
        if (password.equals(customer.Password)) return customer;
        else Show.Message("Login failed");
        return null;
    }

}
